package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import team.yidong.fenda.service.HospitalModuleService;
import team.yidong.fenda.service.HospitalService;
import team.yidong.fenda.utils.ImageCheck;
import team.yidong.fenda.utils.QiNiuYunUtil;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by xuanlong on 2018/5/28.
 */
@Controller
public class HospitalModuleController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalModuleService hospitalModuleService;

    @Autowired
    ImageCheck imageCheck;
    @Autowired
    QiNiuYunUtil qiNiuYunUtil;

    @RequestMapping("/hospitalModule")
    public String hospitalModule(Model model, HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);
        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "hospital", "userSet", "masterList"});
            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
        } else {
            JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
            model.addAttribute("userList", hospitalService.findAllSysUser());
        }

        return "hospitalModule";
    }

    @RequestMapping("/searchHospitalModuleByHospitalId")
    @ResponseBody
    public JSONArray searchByHospitalId(Integer hospitalId) {
        return hospitalModuleService.searchByHospitalId(hospitalId);
    }

    @RequestMapping("/updateHospitalModuleIcon")
    @ResponseBody
    public String updateHospitalModuleIcon(@RequestParam Integer iconId, MultipartFile icon) {
        String fileName = new String();
        if (!icon.isEmpty()) {
            try {
                if (imageCheck.isImage(icon.getBytes())) {
                    // 获取文件的后缀名
                    fileName = icon.getOriginalFilename();
                    String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    //uuid为文件命名
                    fileName = UUID.randomUUID() + suffixName;
                    qiNiuYunUtil.uploadFiles(icon.getBytes(), fileName);
                    hospitalModuleService.updateIcon(iconId, fileName);
                    return "修改成功";
                } else {
                    return "不是图片文件";
                }
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败";
            }
        } else {
            return "文件为空";
        }

    }

    @RequestMapping("/updateHospitalModule")
    @ResponseBody
    public void updateHospitalModule(String jsonData) {
        hospitalModuleService.updateHospitalModule(jsonData);
    }
}
